import SocketInstance from '/@/views/chat/im-server/socket-instance.js'
import { ServeGetUserSetting } from '/@/api/sys/user/index'
import { useUserInfo } from '/@/stores/userInfo'
import { useSocket } from '/@/stores/chat/socket'
import { useChatTalk } from '/@/stores/chat/talk'

export const MainMixin = {
    create() {
        const socketStore = useSocket()
        const chatTalk = useChatTalk()
        if (socketStore.loginStatus) {
            this.initialize()
            chatTalk.LOAD_TALK_ITEMS()
        } 
    },
    methods: {
        // 页面初始化设置
        initialize() {
            SocketInstance.connect()
            this.loadUserSetting()
        },
        // 加载用户相关设置信息，更新本地缓存
        loadUserSetting() {
            const userInfo = useUserInfo()
            ServeGetUserSetting().then(res => {
                if (res.code == 0) {
                    const { user_info } = res.data
                    userInfo.UPDATE_USER_INFO({
                        uid: user_info.uid,
                        nickname: user_info.nickname,
                        sex: user_info.gender,
                        signature: user_info.motto,
                        avatar: user_info.avatar
                    })
                }
            })
        },
        reload() {
            // this.$root.$children[0].refreshView()
        }
    }
}
